
import { RequestOptions } from "../type";
// 后端服务的基础地址
interface Resp{
	message:string
}
const baseUrl = 'http://192.168.72.1:3000';

const request = (options: RequestOptions): Promise<any> => {
  return new Promise((resolve, reject) => {
    
    const fullUrl = `${baseUrl}${options.url.startsWith('/') ? '' : '/'}${options.url}`;
    const token = uni.getStorageSync('token') 
    uni.request({
      url: fullUrl,
      method: options.method || 'GET', // 默认GET方法
      data: options.data || {},
      header: token==''?{ 'Content-Type': 'application/json' }:{ 'Authorization': token } ,
      success: (res) => {
        // 这里可以根据后端返回结构处理成功逻辑
        // 例如：if (res.data.code === 200) resolve(res.data)
		if((res.data as Resp ).message =="请先登录"){
			uni.showModal({
				title: '提示',
				content: '请先登录',
				showCancel: false,
				cancelText: '',
				confirmText: '确定',
				success: res => {
					uni.navigateTo({
						url: '/pages/index/index',
						
					});
				},
			});
		}else if((res.data as Resp ).message =="登录已失效，请重新登录"){
			uni.showModal({
				title: '提示',
				content: '登录已失效，请重新登录',
				showCancel: false,
				cancelText: '',
				confirmText: '确定',
				success: res => {
					uni.navigateTo({
						url: '/pages/index/index',
						
					});
				},
			});
		}
        resolve(res.data);
      },
      fail: (err) => {
		
        // 统一错误处理（可扩展）
        reject(new Error(`请求失败失败失败失败: ${err.errMsg || '未知错误'}`));
		
      }
    });
  });
};

export default request;